package com.yangming.springboot.deepin.playTest;

import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;

/**
 * @创建人 ym
 * @创建时间 2021/4/8 0008
 * @描述
 **/
public class Piped {

    public static void main(String[] args) throws Exception{
        PipedWriter out = new PipedWriter();
        PipedReader in = new PipedReader();
        out.connect(in);
        Thread printThread = new Thread(new Print(in), "PrintThread");
        Thread print2Thread = new Thread(new Print2(in), "printThread2");
        printThread.start();
        print2Thread.start();
        int receive = 0;
        try{
            while((receive = System.in.read()) != -1){
                out.write(receive);
            }
        }finally{
            out.close();
        }
    }

    public static class Print implements Runnable{
        private PipedReader in;
        public Print(PipedReader in){
            this.in = in;
        }
        @Override
        public void run(){
            int receive = 0;
            try{
                while((receive = in.read()) != -1){
                    System.out.print((char) receive);
                }
            }catch(IOException ex){
                ex.printStackTrace();
            }
        }
    }

    public static class Print2 implements Runnable{
        private PipedReader in;
        public Print2(PipedReader in){
            this.in = in;
        }
        @Override
        public void run(){
            int receive = 0;
            try{
                while((receive = in.read()) != 1){
                    System.out.print((char)receive);
                }
            }catch(IOException ex){
                ex.printStackTrace();
            }
        }
    }

}
